<?php

/**
 * Image Testimonials Product View Block
 */
class AW_Imagetest_Block_Product_View extends Mage_Core_Block_Template
{
    /**
     * Path to block template
     */
    const VIEW_TEMPLATE = "aw_custom_image/product/view.phtml";

    /**
     * Save current product model instance
     * @var Mage_Catalog_Model_Product
     */
    protected $_product = null;

    /**
     * Cached collection
     * @var AW_Imagetest_Model_Mysql4_Image_Collection
     */
    protected $_collection = null;
    protected $_previewcollection = null;

    /**
     * Array of thumbnails count for each layout
     * @var array
     */
    protected $_count = array();

    /**
     * Default thumbnails count
     * @var int
     */
    protected $_defaultCount = 4;

    /**
     * Class constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate(self::VIEW_TEMPLATE);
        if (Mage::registry('current_product')){
            $this->_product = Mage::registry('current_product');
        }

    }

    /**
     * Before rendering html, but after trying to load cache
     * @return Mage_Core_Block_Abstract
     */
    protected function _beforeToHtml()
    {
        parent::_beforeToHtml();
        if (!$this->getProduct() && $this->getProductId()){
            $this->_product = Mage::getModel('catalog/product')->load($this->getProductId());
        }
        return $this;
    }

    /**
     * Set up count of thumbnails for every type of layout
     * @param string $layout Layout code
     * @param int|string $count Count of thumbnails that we can show inline
     * @return AW_Imagetest_Block_Product_View
     */
    public function addColumnCountLayoutDepend($layout, $count)
    {
        $this->_count[$layout] = $count;
        return $this;
    }


    public function initRatioThumbsToLayout() {

        // Image count
        $this->addColumnCountLayoutDepend('empty', Mage::helper('imagetest/config')->getThumbsForEmptyColLayout());
        $this->addColumnCountLayoutDepend('one_column', Mage::helper('imagetest/config')->getThumbsForOneColLayout());
        $this->addColumnCountLayoutDepend('two_columns_left', Mage::helper('imagetest/config')->getThumbsForTwoColWlbLayout());
        $this->addColumnCountLayoutDepend('two_columns_right', Mage::helper('imagetest/config')->getThumbsForTwoColWrbLayout());
        $this->addColumnCountLayoutDepend('three_columns', Mage::helper('imagetest/config')->getThumbsForThreeColLayout());
    }
   
    public function addHandleForCurrentLayout() {

        if (!$this->getPageLayout()) {

            $layout = $this->getLayout();
            $currentPageRootTemplate = $layout->getBlock('root')->getTemplate();
            $pageLayouts = Mage::getSingleton('page/config')->getPageLayouts();

            foreach ($pageLayouts as $pageLayout) {
                if ($currentPageRootTemplate == $pageLayout->getTemplate()) {
                    $this->getLayout()->getUpdate()->addHandle($pageLayout->getLayoutHandle());
                }
            }
        }
    }

    /**
     * Retrieve thumbnails count
     * @return int
     */
    public function getThumbnailCount()
    {
        if (!$this->getData('thumbnail_count')) {
           $pageLayout = $this->helper('page/layout')->getCurrentPageLayout();
           if ($pageLayout && $this->getColumnCountLayoutDepend($pageLayout->getCode())) {
                $this->setData(
                    'thumbnail_count',
                    $this->getColumnCountLayoutDepend($pageLayout->getCode())
                );
            } else {
                $this->setData('thumbnail_count', $this->_defaultCount);
            }
        }
        return (int) $this->getData('thumbnail_count');
    }

    /**
     * Remove thumbnails count depends on page layout
     * @param string $pageLayoutCode
     * @return AW_Imagetest_Block_Product_View
     */
    public function removeColumnCountLayoutDepend($pageLayoutCode)
    {
        if (isset($this->_count[$pageLayoutCode])) {
            unset($this->_count[$pageLayoutCode]);
        }

        return $this;
    }

    /**
     * Retrieve thumbnails count depends on page layout
     *
     * @param string $pageLayoutCode
     * @return int|boolean
     */
    public function getColumnCountLayoutDepend($pageLayoutCode)
    {
        if (isset($this->_count[$pageLayoutCode])) {
            return $this->_count[$pageLayoutCode];
        }
        return false;
    }

    /**
     * Retrieve current page layout
     * @return Varien_Object
     */
    public function getPageLayout()
    {
        return $this->helper('page/layout')->getCurrentPageLayout();
    }

    /**
     * Retrives show buttons flag
     * @return boolean
     */
    public function showButtons()
    {
        return !!(count($this->getCollection()) > $this->getThumbnailCount());
    }

    public function getButtons()
    {
        $buttons = array();

        $count = count($this->getCollection());
        $tCount = $this->getThumbnailCount();
        $id = 0;
        $isFirst = true;
        $isLast = false;
        while ($count > 0){
            $count -= $tCount;
            if ($count <= 0){
                $isLast = true;
            }
            $button = new Varien_Object(array(
                'num' => (int) $id +1,
                'id' => (int) $id,
                'left' => (int) $id,
                'first' => $isFirst,
                'last' => $isLast,
            ));
            $buttons[] = $button;
            $id ++;
            $isFirst = false;
        }
        return $buttons;
    }

    /**
     * Retrives current product model instance
     * @return Mage_Catalog_Model_Product
     */
    public function getProduct()
    {
        return $this->_product;
    }

    /**
     * Retrives Section Title
     * @return string
     */
    public function getSectionTitle()
    {
       return Mage::getStoreConfig('imagetest/general/title');
    }

    /**
     * Retrives Rating Status
     * (Use constants from AW_Imagetest_Model_System_Config_Source_Rating)
     * @return int
     */
    public function getRatingStatus()
    {
        return Mage::helper('imagetest')->confRatingStatus();
    }

    /**
     * Flag to show player content
     * @return boolean
     */
    public function hasImages()
    {
        return !!count($this->getCollection());
    }

    /**
     * Retrives YouTube player boundle
     * @return string
     */
    public function getPlayer()
    {
       // return Mage::getSingleton('imagetest/connector')->getApiModel('youtube')->getRenderedPlayer();
    }

     public function getActionUrl()
    {
        return $this->getUrl('imagetest/image/post');
    }

    public function getCopyright()
    {
       // return Mage::getSingleton('imagetest/connector')->getApiModel('youtube')->getRenderedCopyright();
    }


    /**
     * Retrives image collection
     * @return AW_Imagetest_Model_Mysql4_Image_Collection
     */
    public function getCollection()
    {
        if (!$this->_collection){
            # 1. Check image states
           // Mage::helper('imagetest/processing')->check($this->getProduct()->getId());
            # 2. Prepare collection
            $this->_collection = Mage::getModel('imagetest/image')->getCollection()
                    ->addStatusFilter(AW_Imagetest_Model_Image::IMAGE_STATUS_ENABLED)
                  //  ->addStateFilter(AW_Imagetest_Model_Image::IMAGE_STATE_READY)
                    ->addStoreFilter(Mage::app()->getStore()->getId())
                    ->addProductFilter($this->getProduct()->getId())
                    ->setOrderByRate()
                    ;
        }
        return $this->_collection;
    }
     /**
     * Retrives image collection
     * @return AW_Imagetest_Model_Mysql4_Image_Collection
     */
    public function getPreviewCollection()
    {
        if (!$this->_previewcollection){
            # 1. Check image states
           // Mage::helper('imagetest/processing')->check($this->getProduct()->getId());
            # 2. Prepare collection
            $this->_previewcollection = Mage::getModel('imagetest/image')->getCollection()
                    ->addStatusFilter(AW_Imagetest_Model_Image::IMAGE_STATUS_ENABLED)
                    ->addStateFilter(AW_Imagetest_Model_Image::IMAGE_STATE_PREVIEW)
                    ->addStoreFilter(Mage::app()->getStore()->getId())
                    ->addProductFilter($this->getProduct()->getId())
                    ->setOrderByRate()
                    ;
        }
        return $this->_previewcollection;
    }

    /**
     * Retirves Can Show Block flag
     * @return boolean
     */
    public function canShow()
    {
        return !$this->getProduct()->getImagetestEnabled();
    }

    /**
     * Retrives Can User Group upload file on servise
     * @return boolean
     */
    public function canUpload()
    {
        return Mage::helper('imagetest')->canUpload();
    }

    /**
     * Retrives Ajax Url
     * @return string
     */
    public function getAjaxUrl()
    {
        return $this->getUrl('imagetest/image/rate');
    }

    /**
     * Retrives Can Rate flag
     * @param int|string $id Image Id
     * @return int
     */
    public function getCanRate($id)
    {
        return (Mage::helper('imagetest')->isRateRegistered($id) ? 0 : 1)
                && ($this->getRatingStatus() == AW_Imagetest_Model_System_Config_Source_Rating::STATUS_DISPLAY_AND_RATE);
    }

    /**
     * Retrives show rate box flag
     * @return boolean
     */
    public function showRate()
    {
        return $this->getRatingStatus() != AW_Imagetest_Model_System_Config_Source_Rating::STATUS_DISABLED;
    }


    public function prepareToDisplay($text) {

        $escaped = htmlspecialchars($text);
        $escaped = nl2br($escaped);
        $escaped = preg_replace("/[\n\r]/", "", $escaped);

        return $escaped;
    }
    /**
     * retrives Customer Name
     * @return string
     */
    public function getCustomerName()
    {
        if ($customer = Mage::getSingleton('customer/session')->getCustomer()){
            return $customer->getName();
        }
        return null;
    }

    /**
     * retrives Customer Email
     * @return string
     */
    public function getCustomerEmail()
    {
        if ($customer = Mage::getSingleton('customer/session')->getCustomer()){
            return $customer->getEmail();
        }
        return null;
    }

    /**
     * Retrives current product id
     * @return int
     */
    public function getProductId()
    {
        if ($product = Mage::registry('current_product')){
            return $product->getId();
        }
        return null;
    }

}

